public class BinaryTree {

    static class TreeNode {
        public char val;
        //存储左孩子结点的引用
        public TreeNode left;
        //存储右孩子结点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }


    //中序遍历
    public void inOrder(TreeNode root){
        if(root == null) {
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root){
        if(root == null) {
            return ;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    /**
     * 求节点个数  遍历思路
     */
    public int countNode = 0;
    public void nodeSize(TreeNode root) {
        if(root == null) {
            return;
        }
        countNode++;
        nodeSize((root.left));
        nodeSize((root.right));
    }

    /**
     * 求节点个数   子问题
     * @param root
     * @return
     */
    public int nodeSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int tmp = nodeSize2(root.left)
                +nodeSize2(root.right)+1;
        return tmp;
    }

    /**
     * 求叶子节点个数
     * 子问题：左树的叶子+右树的叶子=整棵树的叶子节点  递推公式
     * 叶子：没有左子树 没有右子树
     * @param root
     * @return
     */
     public int getLeafNodeCount(TreeNode root) {
         if (root == null) {
             return 0;
         } else if (root.left == null && root.right == null) {
             return 1;
         } else {
             return getLeafNodeCount(root.left) +
                     (getLeafNodeCount(root.right));
         }
     }

     public int leafNodeSize;

    /**
     * 子问题思路 求叶子节点个数
     * @param root
     */
     public void getLeafNodeCount2(TreeNode root) {
         if(root == null) {
             return ;
         }
         if(root.left == null && root.right == null) {
             leafNodeSize++;
         }
         getLeafNodeCount2(root.left);
         getLeafNodeCount2(root.right);
     }

    /**
     * 第k层节点数的个数
     * @param root
     * @param k
     * @return
     */
     public int getKLeveNodeSize(TreeNode root,int k) {
         if(root == null) {
             return 0;
         }
         if(k == 1) {
             return 1;
         }
         return getKLeveNodeSize(root.left,k-1) +
                 getKLeveNodeSize(root.right,k-1);
     }

    /**
     *求二叉树的高度
     * 时间复杂度 O(n)
     * 空间复杂度 O(logN)
     * @param root
     * @return
     */
     public int getHeight(TreeNode root) {
         if(root == null) {
             return 0;
         }
         int leftH = getHeight(root.left);
         int rightH = getHeight(root.right);
         //return leftH > rightH ? leftH+1 : rightH+1;
         return Math.max(leftH,rightH)+1;
     }


    /**
     * 在root这课树中找到val值
     * @param root
     * @param val
     * @return
     */
     public TreeNode find(TreeNode root,char val) {
         if(root == null) {
             return null;
         }
        if(root.val == val) {
            return root;
        }
        TreeNode leftValue = find(root.left,val);
        if(leftValue != null) {
            return leftValue;
        }
        TreeNode rightValue = find(root.right,val);
        if(rightValue != null) {
            return rightValue;
        }
        return null;
     }


    /**
     * 另一棵树的子树
     * 时间复杂度O(m*n)
     * @param root m
     * @param subRoot n
     * @return
     */
     public boolean isSubtree(TreeNode root,TreeNode subRoot){
         if(root == null) {
             return false;
         }
         if(isSameTree(root,subRoot)) {
             return true;
         }
         if(isSubtree(root.left,subRoot)) {
             return true;
         }
         if(isSubtree(root.right,subRoot)) {
             return true;
         }
         return false;
     }


    /**
     * 两棵二叉树是否相同
     *时间复杂度：O(min(m,n))
     * @param p
     * @param q
     * @return
     */
     public boolean isSameTree(TreeNode p,TreeNode q) {
         if((p == null && q != null) || (p != null && q == null)) {
            return false;
         }
         if(p ==null && q == null) {
             return true;
         }
         if(p.val != q.val) {
             return false;
         }
         return isSameTree(p.left,q.left) && isSameTree(p.right,p.right);
     }
}
